package daemon;

import java.io.File;
import java.io.IOException;
import java.net.*;

import common.*;

/**
 * A class representing a package connection thread.
 * After a successful connection a packages is saved to appropriate directory. 
 */
public class PackageServerConnection implements Runnable {
	String packagePath;
	Socket socket;
	public PackageServerConnection(String packagePath, Socket socket){
		this.packagePath = packagePath;
		this.socket = socket;
	}
	
	public void run(){
	      try {
			java.io.InputStream in = socket.getInputStream();
			java.io.OutputStream os = socket.getOutputStream();
			String fileName = ByteStream.toString(in);
			String filePath = packagePath +"/" + fileName;
			ByteStream.toStream(os, new File(filePath));
		} catch (IOException e) {
			e.printStackTrace();
		}
	     
	}
}
